package com.nemlistapp;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.widget.EditText;

public class CreateUserFetcher extends AsyncTask<String, Void, Integer> {
	
	private String NAMESPACE;
	
	private final String METHOD_NAME = "CreateUser";
	private final String SOAP_ACTION = "http://tempuri.org/IListService/CreateUser";
	
	private String URL;
	private SoapSerializationEnvelope envelope;
	private ProgressDialog progressDialog;
	private Context context;
	private Activity activity;
	private EditText firstName;
	private EditText lastName;
	private EditText userName;
	private EditText email;
	private EditText password;
	private EditText passwordAgain;

	
public CreateUserFetcher(Context context){
		
		this.context = context;
		this.activity = (Activity) this.context;
		
		this.firstName = (EditText)this.activity.findViewById(R.id.createFirstNameField);
		this.lastName = (EditText)this.activity.findViewById(R.id.createLastNameField);
		this.userName = (EditText)this.activity.findViewById(R.id.createUsernameField);
		this.email = (EditText)this.activity.findViewById(R.id.createEmailField);
		this.password = (EditText)this.activity.findViewById(R.id.createPasswordField);
		this.passwordAgain = (EditText)this.activity.findViewById(R.id.createPasswordAgainField);
		
		this.progressDialog = new ProgressDialog(this.context);
		this.progressDialog.setCancelable(false);
		this.progressDialog.setMessage("Operetter bruger");
		this.progressDialog.setTitle("Vent");
		this.progressDialog.setIndeterminate(true);

	}


	protected void onPostExecute(Integer result)
	{   
		if(this.progressDialog != null && this.progressDialog.isShowing()){
			this.progressDialog.dismiss();
		}
		
		switch (result) {
	        case 1: 
	        	//loginText.setText("Brugeren er oprettet login her");
	            Intent intent = new Intent(this.context, LoginActivity.class);
	            States.getInstance().setCreateUserState(-1);
	            activity.startActivity(intent);
	        	break;
	        case 2: 
	        	this.userName.setError("Brugernavnet er brugt");
	        	States.getInstance().setCreateUserState(-1);
	        	break;
	        case 3: 
	        	this.email.setError("Email er brugt");
	        	States.getInstance().setCreateUserState(-1);
	        	break;
	        default: 
		}
		
	}
	
	@Override
	protected void onPreExecute() {
		this.progressDialog.show();
		
	}

	@Override
	protected Integer doInBackground(String...params) {
		
		String fname = params[0];
		String lname = params[1];
		String uname = params[2];
		String email = params[3];
		String pass = params[4];
		
		NAMESPACE = context.getString(R.string.ws_namespace);
		URL = context.getString(R.string.ws_url);
		
		SoapObject request  = new SoapObject(NAMESPACE,METHOD_NAME);
		
		PropertyInfo firstnameProperty = new PropertyInfo();
		PropertyInfo lastnameProperty = new PropertyInfo();
		PropertyInfo emailProperty = new PropertyInfo();
		PropertyInfo usernameProperty = new PropertyInfo();
		PropertyInfo passwordProperty = new PropertyInfo();
		
		firstnameProperty.setName("fname");
		firstnameProperty.setValue(fname);
		firstnameProperty.setType(String.class);
		
		lastnameProperty.setName("lname");
		lastnameProperty.setValue(lname);
		lastnameProperty.setType(String.class);
		
		usernameProperty.setName("uname");
		usernameProperty.setValue(uname);
		usernameProperty.setType(String.class);
		
		emailProperty.setName("email");
		emailProperty.setValue(email);
		emailProperty.setType(String.class);
		
		passwordProperty.setName("pass");
		passwordProperty.setValue(pass);
		passwordProperty.setType(String.class);
		
		request.addProperty(firstnameProperty);
		request.addProperty(lastnameProperty);
		request.addProperty(usernameProperty);
		request.addProperty(emailProperty);
		request.addProperty(passwordProperty);
		
		envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.implicitTypes = true;
		envelope.setOutputSoapObject(request);
		
		int result = 6;
		HttpTransportSE httpRequest = new HttpTransportSE(URL);
		
		try{
			
			httpRequest.call(SOAP_ACTION, envelope);
			SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
			result = Integer.parseInt(response.toString());
			
		} catch (Exception e){
			e.printStackTrace();
		}
		
		States.getInstance().setCreateUserState(result);
		return result;
		
	}

}
